<template>
  <div class="main-container">
    <div class="login-form">
      <el-form :rules="rules" :model="registerForm" ref="registerRef">
        <div class="title-container">
          <h1 class="title">猪猪注册</h1>
        </div>
        <el-form-item
          style="margin-left: 60px; margin-right: 60px"
          class="form-item"
          prop="account"
        >
          <el-input
            placeholder="请输入账号"
            :prefix-icon="User"
            v-model="registerForm.account"
          ></el-input>
        </el-form-item>
        <el-form-item
          style="margin-left: 60px; margin-right: 60px"
          class="form-item"
          prop="password"
        >
          <el-input
            type="password"
            placeholder="请输入密码"
            :prefix-icon="Lock"
            v-model="registerForm.password"
          />
        </el-form-item>
        <el-form-item
          style="margin-left: 60px; margin-right: 60px"
          class="form-item"
          prop="chirmPass"
        >
          <el-input
            type="password"
            placeholder="请确认密码"
            :prefix-icon="Lock"
            v-model="registerForm.chirmPass"
          />
        </el-form-item>

        <!-- 登录按钮 -->
        <el-button
          type="primary"
          style="margin-left: 61px;width: 107px; border-radius: 0px; margin-top: 15px"
          @click="register()"
          >注册</el-button
        >
        <el-button
          type="info"
          style="margin-left: 0px;width:107px; border-radius: 0px; margin-top: 15px"
          @click="back()"
          >返回</el-button
        >
      </el-form>
    </div>
  </div>
</template>

<script >
import { User, Lock } from "@element-plus/icons-vue";
import { register } from "../api";
export default {
  beforeCreate() {
    window.document.body.setAttribute("style", "background-color:black");
  },
  methods: {
    back() {
      this.$router.push("/");
    },
    register() {
      this.$refs["registerRef"].validate((valid) => {
        if (valid) {
          if (this.registerForm.chirmPass != this.registerForm.password) {
            this.$message.error("密码输入不一致");
          } else {
            this.formline.account=this.registerForm.account;
            this.formline.create_at=new Date();
            this.formline.password = this.registerForm.password;
            register(this.formline).then((res)=>{
              console.log(res);
              if(res.code=="200"){
                this.$message.success("注册成功")
                this.$router.push("/")
              }else{
                this.$message.error(res.message)
              }
            })
          }
        }
      });
    },
  },
  data() {
    return {
      registerForm: {
        account: "",
        password: "",
        chirmPass: "",
      },
      formline:{
        account:"",
        password:"",
        create_at:"",
      },
      rules: {
        account: [
          {
            required: true,
            message: "请输入账号",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: "blur",
          },
        ],
        chirmPass: [
          {
            required: true,
            message: "请确认你的密码",
            trigger: "blur",
          },
        ],
      },
      Lock,
      User,
    };
  },
};
</script>

<style lang="scss">
.main-container {
  margin-left: 320px;
  margin-top: 150px;
  width: 1200px;
  height: 675px;
  background-image: url("../assets/img/75cf4c51f2a45d806f424d9d6b6985d1.jpg");
}

//登录表单样式
.login-form {
  width: 28%;
  height: 330px;
  background-color: #eaecf0;
  float: left;
  margin: 160px 0px 0px 442px;
  //边框圆角
  border-radius: 20px;

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: black;
      margin: 15px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }
}
</style>